import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class GetPanel extends JPanel implements ActionListener {
    private Icon bgOff = Util.getIcon("/images/buttonOff.jpg");
    private Icon bgsure = Util.getIcon("/images/buttonSure.jpg");
    private ATM frame;
    private JTextField textField = new JTextField();
    private JButton offButton = new JButton(bgOff);
    private JButton sureButton = new JButton(bgsure);

    public GetPanel(ATM frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        sureButton.setBounds(300, 500, 100, 50);
        this.add(sureButton);
        sureButton.addActionListener(this);
        JLabel label = new JLabel("取款金额");
        label.setBounds(350, 300, 100, 20);
        this.add(label);
        textField.setBounds(400, 300, 100, 20);
        this.add(textField);
        offButton.setBounds(300, 600, 100, 50);
        this.add(offButton);
        offButton.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == offButton) {
            frame.switchPanel("menu");
        } else if (e.getSource() == sureButton) {
            ArrayList<User> list= (ArrayList<User>) FileUtil.readObject("c:/a.txt");
            double yue = frame.getList().get(frame.getLoginPanel().getIndex()).getMoney();
            double shuru = Double.valueOf(textField.getText());
            if (shuru > yue) {
                JOptionPane.showMessageDialog(this, "余额不足");
            } else {
                double qu = yue - shuru;
                frame.getList().get(frame.getLoginPanel().getIndex()).setMoney(qu);
                FileUtil.writeObject("c:/a.txt",list);
                frame.switchPanel("menu");
            }

        }
    }
}
